/**
 * 实际费用核销
 * @author mengyb
 * @date 2019/10/25
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_gwax = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWFEDOCNO', width: 150, title: '实际费用核销单号', fixed: 'left',sort: true}
            , {field: 'GWFESEQ', width: 60, title: '项次'}
            , {field: 'GWFE001', width: 60, title: '年度',sort: true,search: 'default',searchOpts:{type:'year'}}
            , {field: 'GWFE002', width: 60, title: '月份',sort: true,search: 'default',searchOpts:{type:'MM'}}
            , {field: 'GWFE003_NAME', width: 80, title: '区域',search:'default',searchOpts:{icon:true,readonly:true}}
            , {field: 'GWFE004_NAME', width: 120, title: '客户',search:'default',searchOpts:{icon:true,readonly:true}}
            , {field: 'GWFE005_NAME', width: 120, title: '品类',search:'default',searchOpts:{icon:true,readonly:true}}
            , {field: 'GWFE006_NAME', width: 100, title: '业务组织',search:'default',searchOpts:{icon:true,readonly:true}}
            , {field: 'GWFE007_NAME', width: 100, title: '费用'}
            , {field: 'GWFE008', width: 100, title: '费用核销方式',filterDatas: app.datas_('crm_gwft400_gwfe008')}
            , {field: 'GWFE009', width: 130, title: '实际费用计算单号'}
            , {field: 'GWFE010', width: 60, title: '项次'}
            , {field: 'GWFE100_NAME', width: 80, title: '币别'}
            , {field: 'GWFE101', width: 60, title: '汇率'}
            , {field: 'GWFE102', width: 120, title: '区域核销费用原币'}
            , {field: 'GWFE103', width: 120, title: '区域核销费用本币'}
            , {field: 'GWFE106', width: 120, title: '区域核销差异原因'}
            , {field: 'GWFE104', width: 120, title: '总部核销费用原币'}
            , {field: 'GWFE105', width: 120, title: '总部核销费用本币'}
            , {field: 'GWFE107', width: 120, title: '总部核销差异原因'}
            , {field: 'GWFEACTI', width: 80, title: '审核码',filterDatas: app.datas_('check_code')}
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwft400setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwft400SettingAction,
        moduleName='实际费用核销'
    ;

    var Gwft400 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwft400',
                _searchs: [{
                    id: 'gwft400', tableId: 'gwft400_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {dom: 'gwft400_edit', tpl: 'gwft400_edit'},
                    {dom: 'common_open_window', tpl: 'common/_open_window'},
                    {dom: 'gwft400_costcheck', tpl: 'gwft400_costcheck'}
                    ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwft400_table'),
                    url: g_ctxPath + 'crm/gwft400/getList',
                    title: '实际费用核销',
                    method: 'post',
                    id: 'gwft400_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwft400setting-action').innerHTML).render({}, function (string) {
                    gwft400SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwft400_table)', function (obj) {

                    layer.tips(gwft400SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if(obj.data.GWFEACTI != 'N'  ){
                                            app.showError("已审核的资料无法修改！");
                                            return ;
                                        }
                                        self._openTplFull({
                                            tag: 'gwft400_edit',
                                            tagData:{
                                                moduleName:moduleName,
                                                obj:obj,
                                                isAdd:false,
                                                isCheck:false
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                minmax:true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWFEACTI != 'N'  ){
                                            app.showError("已审核的资料无法删除！");
                                            return ;
                                        }
                                        self.delRow(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwft400_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwft400_edit',
                        tagData: {
                            moduleName:moduleName,
                            obj: obj,
                            isAdd: false,
                            isCheck: true,
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '详情',
                            minmax:true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwft400_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwft400_edit',
                                tagData: {
                                    moduleName:moduleName,
                                    isAdd: true,
                                    isCheck:false
                                },
                                index: 0,
                                openOpts: {
                                    title: '新增'+moduleName,
                                    minmax:true
                                }
                            });
                            break;
                        case 'del':
                            self.delRows();
                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                        case 'costCheck':
                            self.costCheck();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwft400_table', where: table_gwax});
        },

        delRow: function (obj) {
            app.confirm({
                msg:'确认删除该条数据吗？',
                ok:function () {
                    app.ajax({
                        xurl: 'crm/gwft400/delRow',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName +'信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            })
        },

        delRows: function(){
            var checkStatus = table.checkStatus('gwft400_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if(v.GWFEACTI == 'N'){
                        var _temp = app.fields_('GWFE_T');
                        _temp.GWFEDOCNO = v.GWFEDOCNO;
                        // _temp.GWFESEQ = v.GWFESEQ;
                        _xdata.push(_temp);
                    }else{
                        app.showError("已审核的资料无法删除！");
                        flag = false;
                        return ;
                    }

                });
                if(flag){
                    //对象数组去重
                    _xdata=_xdata.reduce(function (pre,cur) {
                        var findResult=pre.find(function (item) {
                            return item.GWFEDOCNO == cur.GWFEDOCNO
                        })
                        if(findResult == undefined){
                            pre.push(cur)
                        }
                        return pre;
                    },[]);
                    app.confirm({
                        msg:'是否删除所选择的数据？',
                        ok: function () {
                            app.ajax({
                                xurl:'crm/gwft400/delRows',
                                xdata:_xdata,
                                toJsonStr:true,
                                okmsg:moduleName+'信息删除成功',
                                loading:true,
                                cb: function (result) {
                                    if (result.ok) {
                                        self.reloadTableCurr();
                                    }
                                }
                            })
                        }
                    });
                }
            }else{
                app.showError('请先选择待删除的数据');
            }
        },

        //审核
        check: function () {
            var checkStatus = table.checkStatus('gwft400_table');
            if (checkStatus.data.length > 0) {
                var _xdata=[];
                var flag=true;//是否满足确认条件
                checkStatus.data.forEach(function (v) {
                    if (v.GWFEACTI == 'N') {
                        var _xtemp=app.fields_('GWFE_T');
                        _xtemp.GWFEDOCNO=v.GWFEDOCNO;
                        // _xtemp.GWFESEQ=v.GWFESEQ;
                        _xdata.push(_xtemp);
                    }else {
                        flag=false;
                        return;
                    }
                });
                if(flag){//满足审核条件
                    //对象数组去重
                    _xdata=_xdata.reduce(function (pre,cur) {
                        var findResult=pre.find(function (item) {
                            return item.GWFEDOCNO == cur.GWFEDOCNO
                        })
                        if(findResult == undefined){
                            pre.push(cur)
                        }
                        return pre;
                    },[]);
                    app.ajax({
                        xurl:'crm/gwft400/check',
                        xdata:_xdata,
                        loading:true,
                        toJsonStr:true,
                        confirm: {msg: "是否审核选择的数据？"},
                        okmsg: moduleName + '信息审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }

                        }
                    })
                }else {//不满足审核条件
                    app.showError('不可重复审核');
                }
            }else{
                app.showError('请先选择待审核的数据');
            }
        },
        //取消审核
        unCheck: function () {
            var checkStatus = table.checkStatus('gwft400_table');
            if (checkStatus.data.length > 0) {
                var _xdata=[];
                var flag=true;//是否满足取消确认条件
                checkStatus.data.forEach(function (v) {
                    if (v.GWFEACTI == 'Y') {
                        var _xtemp=app.fields_('GWFE_T');
                        _xtemp.GWFEDOCNO=v.GWFEDOCNO;
                        // _xtemp.GWFESEQ=v.GWFESEQ;
                        _xdata.push(_xtemp);
                    }else {
                        flag=false;
                        return;
                    }
                });
                if (flag) {//满足取消审核条件
                    //对象数组去重
                    _xdata=_xdata.reduce(function (pre,cur,curIndex) {
                        var findResult=pre.find(function (item) {
                            return item.GWFEDOCNO == cur.GWFEDOCNO
                        })
                        if(findResult == undefined){
                            pre.push(cur)
                        }
                        return pre;
                    },[]);
                    app.ajax({
                        xurl: 'crm/gwft400/unCheck',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否取消审核选择的数据？"},
                        okmsg: moduleName + '信息取消审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                } else {//不满足取消审核条件
                    app.showError('不可重复取消审核');
                }
            }else{//未选择数据
                app.showError('请先选择待取消审核的数据');
            }
        },

        costCheck:function () {
            var vue = this.vues.gwft400;
            var checkStatus = table.checkStatus('gwft400_table');
            if (checkStatus.data.length > 0) {
                var _xdata=[]; var _ydata=[]; var _adata=[];
                var flag=true;//是否满足确认条件
                checkStatus.data.forEach(function (v) {
                    if (v.GWFEACTI == 'N') {
                        var _xtemp = app.fields_('GWFE_T');
                        _xtemp.GWFE003 = v.GWFE003;
                        _xdata.push(_xtemp);
                        var _ytemp = app.fields_('GWFE_T');
                        _ytemp.GWFE004 = v.GWFE004;
                        _ydata.push(_ytemp);
                        var _atemp = app.fields_('GWFE_T');
                        _atemp.GWFE003_NAME = v.GWFE003_NAME;
                        _adata.push(_atemp);
                    }
                });
                //对象数组去重
                _xdata=_xdata.reduce(function (pre,cur) {
                    var findResult=pre.find(function (item) {
                        return item.GWFE003 == cur.GWFE003;
                    })
                    if(findResult == undefined){
                        pre.push(cur)
                    }
                    return pre;
                },[]);
                _ydata=_ydata.reduce(function (pre,cur) {
                    var findResult=pre.find(function (item) {
                        return item.GWFE004 == cur.GWFE004;
                    })
                    if(findResult == undefined){
                        pre.push(cur)
                    }
                    return pre;
                },[]);
                _adata=_adata.reduce(function (pre,cur) {
                    var findResult=pre.find(function (item) {
                        return item.GWFE003_NAME == cur.GWFE003_NAME;
                    })
                    if(findResult == undefined){
                        pre.push(cur)
                    }
                    return pre;
                },[]);
                let areas = "";
                _adata.forEach(function (item) {
                    areas = areas + item.GWFE003_NAME + ",";
                })
                app.ajax({
                    xurl:'crm/gwft400/getCostCheckData',
                    xdata:{_xdata:_xdata, _ydata: _ydata, GWEF001: vue.isearch.fields.GWFE001,
                        GWEF002: vue.isearch.fields.GWFE002, GWEF003: vue.isearch.fields.GWFE003, areas: areas},
                    loading:true,
                    toJsonStr:true,
                    cb: function (result) {
                        if (result.ok) {
                            self._openTplFull({
                                tag: 'gwft400_costcheck',
                                tagData: {
                                    list: result.data.data.list
                                },
                                index: 2,
                                openOpts: {
                                    title: '费用耗用核查',
                                    area: ['920px', '550px']
                                }
                            });
                        }

                    }
                })
            }else{
                app.showError('请先选择数据');
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwft400',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWFE003_NAME') {//区域开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwad002',
                                            columnList: [
                                                {label: '区域编号', prop: 'GWAD002', width: '150'},
                                                {label: '区域名称', prop: 'GWADL004', width: '150'},
                                                {label: '助记码', prop: 'GWADL005', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFE003 = data.GWAD002;
                                            vue.isearch.fields.GWFE003_NAME = data.GWADL004;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择区域',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if(field == 'GWFE004_NAME'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb:function (data) {
                                            vue.isearch.fields.GWFE004 = data.GWAK001;
                                            vue.isearch.fields.GWFE004_NAME = data.GWAKL004;
                                        }
                                    },
                                    index:1,
                                    openOpts: {
                                        title: '选择客户',
                                        area:['600px', '465px']
                                    }
                                });
                            }else if(field == 'GWFE005_NAME'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwao001',
                                            columnList: [{label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                                {label: '说明', prop: 'GWAOL003', width: '150'},
                                                {label: '助记码', prop: 'GWAOL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFE005 = data.GWAO001;
                                            vue.isearch.fields.GWFE005_NAME = data.GWAOL003;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择品类',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if(field == 'GWFE006_NAME'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysorg_id',
                                            columnList: [
                                                {label: '组织编号', prop: 'ID', width: '150'},
                                                {label: '组织名称', prop: 'NAME', width: '150'}
                                            ],
                                            searchParam: {
                                                TYPE:'9'
                                            }
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFE006 = data.ID;
                                            vue.isearch.fields.GWFE006_NAME = data.NAME;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择组织',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    },
                    watch:{
                        'isearch.fields.GWFE003_NAME': function (val) {
                            if (val == '' || val == null) {
                                this.isearch.fields.GWFE003 = null;
                            }
                        },
                        'isearch.fields.GWFE004_NAME': function (val) {
                            if (val == '' || val == null) {
                                this.isearch.fields.GWFE004 = null;
                            }
                        },
                        'isearch.fields.GWFE005_NAME': function (val) {
                            if (val == '' || val == null) {
                                this.isearch.fields.GWFE005 = null;
                            }
                        },
                        'isearch.fields.GWFE006_NAME': function (val) {
                            if (val == '' || val == null) {
                                this.isearch.fields.GWFE006 = null;
                            }
                        },
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwft400();

});
